package tags;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import statics.SoptVars;

import com.ibatis.sqlmap.client.SqlMapClient;

import comm.util.RequestManager;
import comm.util.SoptHashMap;
import comm.util.SqlMapLoader;

public class WagleTagManager {
	
	private SqlMapClient sqlMapper = SqlMapLoader.getInstance();
	
	public WagleTagManager() {
		// TODO Auto-generated constructor stub
	}
	
	public ArrayList getTodayTags(){
		ArrayList lt = null;
		try {
			lt = (ArrayList) sqlMapper.queryForList("tag.getTodayTags");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lt;
	}
	
	public ArrayList getTagsByDay(String date){
		ArrayList lt = null;
		try {
			lt = (ArrayList) sqlMapper.queryForList("tag.getTagsByDay",date);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lt;
	}
	
	public SoptHashMap regContentForTag(HttpServletRequest request) throws UnsupportedEncodingException{
		SoptHashMap hm = RequestManager.getRequest(request);
		
		if(hm.get("TAG_SEQ")!=null && hm.get("CONTENT")!=null && request.getSession().getAttribute("SESSION_MEMBER_SEQ")!=null && hm.get("CONTENT").toString().trim().length()>0){
			hm.put("MEMBER_SEQ", request.getSession().getAttribute("SESSION_MEMBER_SEQ"));
			try {
				Object dbResult = sqlMapper.insert("tag.regContentForTag",hm);
				if(dbResult!=null){
					hm.clear();
					hm.put("RESULT", SoptVars.SUCCESS);
					hm.put("ERROR_MSG", "저장되었습니다.");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				hm.clear();
				hm.put("RESULT", SoptVars.FAIL);
				hm.put("ERROR_MSG", "저장 도중 오류가 발생했습니다.");
			}
		}else{
			hm.clear();
			hm.put("RESULT", SoptVars.FAIL);
			hm.put("ERROR_MSG", "로그인 되지 않았거나, 내용이 정상적으로 전달되지 못했습니다.");
		}
		
		return hm;
	}
	
	public ArrayList getContentsListForTagSeq(String tagSeq){
		ArrayList lt = null;
		try {
			lt = (ArrayList) sqlMapper.queryForList("tag.getContentsListForTagSeq",tagSeq);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lt;
	}
}
